package com.sskj.aikeeper.hangqing.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sskj.aikeeper.R;
import com.sskj.aikeeper.bean.NoticeListBean;
import com.sskj.aikeeper.presenter.NoticePresenter;
import com.sskj.common.base.App;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.PageBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;

/**
 * @author Hey
 * Create at  2019/06/25
 */
public class NoticeActivity extends BaseActivity<NoticePresenter> {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.smartRefreshView)
    SmartRefreshLayout smartRefreshView;
    private BaseAdapter<NoticeListBean> adapter;

    private int page = 1;
    private int size = 10;

    @Override
    public int getLayoutId() {
        return R.layout.app_activity_notice;
    }

    @Override
    public NoticePresenter getPresenter() {
        return new NoticePresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText(App.INSTANCE.getString(R.string.app_noticeActivity1));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BaseAdapter<NoticeListBean>(R.layout.app_item_notice, null, recyclerView, true) {
            @Override
            public void bind(ViewHolder holder, NoticeListBean item) {
                holder.setText(R.id.tv_date, TimeFormatUtil.formatE(Long.valueOf(item.getAddtime()) * 1000))
                        .setText(R.id.tv_title, item.getReward_type())
                        .setText(R.id.tv_type, App.INSTANCE.getString(R.string.app_noticeActivity2))
                        .setText(R.id.tv_content, item.getReward_content());
            }
        };
//        Observable.rangeLong(0, 6)
//                .map(i -> i + "")
//                .toList()
//                .subscribe(strings -> adapter.setNewData(strings));
    }

    @Override
    public void initData() {
        mPresenter.getNoticeList(true, page, size);
        smartRefreshView.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getNoticeList(false, page, size);
        });
        smartRefreshView.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.getNoticeList(false, page, size);
        });
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, NoticeActivity.class);
        context.startActivity(intent);
    }

    public void getNoticeListOK(List<NoticeListBean> data) {
        if (data == null) {
            return;
        }
        if (page == 1) {
            adapter.setNewData(data);
        } else {
            adapter.addData(data);
        }
    }

    public void stopRefresh() {
        smartRefreshView.finishRefresh();
        smartRefreshView.finishLoadMore();
    }
}
